package com.bjqg.bjqg01.bjqg.activity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import com.bjqg.bjqg01.bjqg.R;
import com.bjqg.bjqg01.bjqg.appli.MyApplication;
import com.bjqg.bjqg01.bjqg.utils.Filed;
import com.bjqg.greendao.SeekDao;
import com.hjq.permissions.OnPermission;
import com.hjq.permissions.XXPermissions;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity {
    List<String> list = new ArrayList<>();
    public static String[] strArr;
    SeekDao seekDao;
    private static final int MY_PERMISSIONS_LOCATION = 100;
    private PackageInfo info;
    private Handler goHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            if (Build.VERSION.SDK_INT >= 23) {
                if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION)
                        != PackageManager.PERMISSION_GRANTED
                        || ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION)
                        != PackageManager.PERMISSION_GRANTED
                        || ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                        != PackageManager.PERMISSION_GRANTED
                        || ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE)
                        != PackageManager.PERMISSION_GRANTED
                        || ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_PHONE_STATE)
                        != PackageManager.PERMISSION_GRANTED) {
                    //申请WRITE_EXTERNAL_STORAGE权限
                    ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,
                            Manifest.permission.ACCESS_FINE_LOCATION,
                            Manifest.permission.WRITE_EXTERNAL_STORAGE,
                            Manifest.permission.READ_EXTERNAL_STORAGE,
                            Manifest.permission.READ_PHONE_STATE,
                            Manifest.permission.READ_CONTACTS}, MY_PERMISSIONS_LOCATION);//自定义的code
                } else {
                    Intent intent = new Intent();
                    intent.setClass(MainActivity.this, LoginActivity.class);
                    startActivity(intent);
                    finish();
                }
            } else {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, LoginActivity.class);
                startActivity(intent);
                finish();
            }


            XXPermissions.with(MainActivity.this)
                    .constantRequest()
                    .request(new OnPermission() {
                        @Override
                        public void hasPermission(List<String> granted, boolean isAll) {

                        }

                        @Override
                        public void noPermission(List<String> denied, boolean quick) {
                        }
                    });

        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        EventBus.getDefault().register(this);
        init();
        goHandler.sendEmptyMessage(0);

//        http://api.map.baidu.com/geoconv/v1/?coords=116.311528,40.057022&from=1&to=5&ak=zacCTbC45CRqHa9cA0HVwLY1mngTU63x
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        //如果请求状态码保持一致
        if (requestCode == MY_PERMISSIONS_LOCATION) {
            //获取请求结果
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // Permission Allow
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, LoginActivity.class);
                startActivity(intent);
                finish();
//                Toast.makeText(MainActivity.this, "给予权限", Toast.LENGTH_SHORT).show();
            } else {
                // Permission Denied
                Toast.makeText(MainActivity.this, "请在设置中给予应用权限", Toast.LENGTH_SHORT).show();
                finish();
            }
            return;
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    private void initView() {
        TextView versionCode = (TextView) findViewById(R.id.vc);
        versionCode.setText("版本号:" + getVersionCode());
    }

    //版本号
    public String getVersionCode() {
        try {
            info = getApplicationContext().getPackageManager().getPackageInfo(getApplicationContext().getPackageName(), PackageManager.GET_CONFIGURATIONS);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return info.versionName;
    }


    /*初始化方法*/
    public void init() {
        Log.d("abcde", "init: 执行刷新");
        seekDao = SeekDao.getInstance(this);
        if (seekDao.getBlackNumCount() > 0) {
            for (int i = 0; i < 10; i++) {
                String name = seekDao.getBlackNumByPage(0, 10).get(i).number;
                list.add(name);
                Log.d("abcde", "数据库信息: " + name);

            }
            strArr = new String[list.size()];
            list.toArray(strArr);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(Integer event) {
        if (event == Filed.Reshnr) {
            init();
        }
    }

}
